package com.whfc.fmam.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.whfc.common.enums.DelFlag;
import com.whfc.common.exception.BizException;
import com.whfc.common.result.ListData;
import com.whfc.common.result.PageData;
import com.whfc.common.result.ResultEnum;
import com.whfc.common.util.PageUtil;
import com.whfc.fmam.dao.FmamLicenseMapper;
import com.whfc.fmam.dao.FmamWeighRoomFvsDeviceMapper;
import com.whfc.fmam.dao.FmamWeighRoomMapper;
import com.whfc.fmam.entity.bo.FmamWeighRoom;
import com.whfc.fmam.entity.bo.FmamWeighRoomFvsDevice;
import com.whfc.fmam.entity.dto.FmamWeighRoomDTO;
import com.whfc.fmam.entity.dto.FmamWeighRoomFvsDeviceDTO;
import com.whfc.fmam.entity.dto.LicenseFileDTO;
import com.whfc.fmam.entity.qo.FvsDeviceQO;
import com.whfc.fmam.entity.qo.WeighRoomAddQO;
import com.whfc.fmam.entity.qo.WeighRoomEditQO;
import com.whfc.fmam.entity.qo.WeighRoomFvsDeviceAddQO;
import com.whfc.fmam.service.FmamWeighRoomService;
import org.apache.dubbo.config.annotation.DubboService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author sun_guodong
 * @description
 * @date 2019-11-13
 */
@DubboService(interfaceClass = FmamWeighRoomService.class, version = "1.0.0")
public class FmamWeighRoomServiceImpl implements FmamWeighRoomService {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private FmamWeighRoomMapper fmamWeighRoomMapper;

    @Autowired
    private FmamLicenseMapper fmamLicenseMapper;

    @Autowired
    private FmamWeighRoomFvsDeviceMapper fmamWeighRoomFvsDeviceMapper;

    @Override
    public PageData<FmamWeighRoomDTO> listFmamWeighRoomDTO(Integer pageNum, Integer pageSize, Integer deptId) throws BizException {
        logger.info("查询磅房列表服务，pageNum:{},pageSize:{},deptIds:{}", pageNum, pageSize, deptId);
        PageHelper.startPage(pageNum, pageSize);
        List<FmamWeighRoomDTO> list = fmamWeighRoomMapper.selectFmamWeighRoomByDeptId(deptId);
        PageHelper.clearPage();
        return PageUtil.pageData(PageInfo.of(list));
    }

    @Override
    public ListData<FmamWeighRoomDTO> listFmamWeighRoomDTO(Integer deptId) throws BizException {
        logger.info("查询磅房列表（不分页）服务,deptIds:{}", deptId);
        List<FmamWeighRoomDTO> list = fmamWeighRoomMapper.selectFmamWeighRoomByDeptId(deptId);
        return new ListData<>(list);
    }

    @Override
    public List<FmamWeighRoomDTO> listWeighRoom(Integer deptId) {
        return fmamWeighRoomMapper.selectFmamWeighRoomByDeptId(deptId);
    }

    @Override
    public List<Integer> listWeighRoomId(Integer deptId) {
        return fmamWeighRoomMapper.selectWeighRootIdByDeptId(deptId);
    }

    @Override
    public FmamWeighRoomDTO getWeighRoom(Integer weighRoomId) {
        FmamWeighRoom fmamWeighRoom = fmamWeighRoomMapper.selectByPrimaryKey(weighRoomId);
        FmamWeighRoomDTO weighRoomDTO = new FmamWeighRoomDTO();
        BeanUtils.copyProperties(fmamWeighRoom, weighRoomDTO);
        return weighRoomDTO;
    }

    @Override
    public void addWeighRoom(WeighRoomAddQO request) throws BizException {
        logger.info("新增磅房服务，WeighRoomAddQO：{}", request.toString());
        String code = request.getCode();
        Integer deptId = request.getDeptId();
        //磅房编码不重复
        FmamWeighRoom fmamWeighRoom = fmamWeighRoomMapper.selectByCodeAndDeptId(code, deptId);
        if (fmamWeighRoom != null) {
            throw new BizException(ResultEnum.FAILURE.getCode(), "磅房编码重复");
        }
        FmamWeighRoom insertSelective = new FmamWeighRoom();
        BeanUtils.copyProperties(request, insertSelective);
        fmamWeighRoomMapper.insertSelective(insertSelective);
    }

    @Override
    public void delWeighRoom(Integer id) throws BizException {
        logger.info("删除磅房服务，id：{}", id);
        fmamWeighRoomMapper.deleteLogicById(id);
    }

    @Override
    public void editWeighRoom(WeighRoomEditQO request) throws BizException {
        logger.info("编辑磅房服务，WeighRoomEditQO：{}", request.toString());
        String code = request.getCode();
        Integer id = request.getId();
        Integer deptId = request.getDeptId();
        //磅房编码不重复
        FmamWeighRoom fmamWeighRoom = fmamWeighRoomMapper.selectByCodeAndDeptId(code, deptId);
        if (fmamWeighRoom != null && !id.equals(fmamWeighRoom.getId())) {
            throw new BizException(ResultEnum.FAILURE.getCode(), "磅房编码重复");
        }
        FmamWeighRoom record = new FmamWeighRoom();
        BeanUtils.copyProperties(request, record);
        record.setUpdateTime(new Date());
        fmamWeighRoomMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public ListData<LicenseFileDTO> licenseFileList(Integer weighRoomId) throws BizException {
        logger.info("查看磅房授权文件服务，weighRoomId：{}", weighRoomId);
        List<LicenseFileDTO> list = fmamLicenseMapper.selectLicenseFileList(weighRoomId);
        return new ListData(list);
    }


    @Override
    public List<FmamWeighRoomFvsDeviceDTO> fvsDeviceList(Integer weighRoomId) throws BizException {
        logger.info("查看磅房监控列表，weighRoomId：{}", weighRoomId);
        List<FmamWeighRoomFvsDeviceDTO> fmamWeighRoomFvsDeviceDTOS = fmamWeighRoomFvsDeviceMapper.selectFvsDeviceListByWeighRoomId(weighRoomId);
        return fmamWeighRoomFvsDeviceDTOS;
    }

    @Override
    public void addWeighRoomFvsDevice(WeighRoomFvsDeviceAddQO fvsDeviceAddQO) throws BizException {
        logger.info("添加磅房监控，fvsDeviceAddQO：{},", fvsDeviceAddQO.toString());
        Integer weighRoomId = fvsDeviceAddQO.getWeighRoomId();
        List<FvsDeviceQO> deviceAddList = fvsDeviceAddQO.getFvsDeviceList();
        //待添加的设备
        Map<Integer, String> addDeviceMap = new HashMap<>();
        if (deviceAddList != null && deviceAddList.size() > 0) {
            for (FvsDeviceQO fvsDeviceQO : deviceAddList) {
                if (!addDeviceMap.containsKey(fvsDeviceQO.getFvsDeviceId())) {
                    addDeviceMap.put(fvsDeviceQO.getFvsDeviceId(), fvsDeviceQO.getFvsDeviceName());
                }
            }
        }

        //查询数据库中磅房监控列表
        List<FmamWeighRoomFvsDeviceDTO> fvsDeviceDTOS = fmamWeighRoomFvsDeviceMapper.selectFvsDeviceListByWeighRoomId(weighRoomId);
        if (fvsDeviceDTOS != null && fvsDeviceDTOS.size() > 0) {
            for (FmamWeighRoomFvsDeviceDTO fvsDeviceDTO : fvsDeviceDTOS) {
                Integer deviceId = fvsDeviceDTO.getFvsDeviceId();
                if (addDeviceMap.containsKey(deviceId)) {
                    //数据库中已存在，从addDeviceMap 中移除
                    addDeviceMap.remove(deviceId);
                } else {
                    //数据库中已存在，但不在添加列表，执行删除操作
                    fmamWeighRoomFvsDeviceMapper.deleteLogicByWeighRoomIdAndDeviceId(weighRoomId, deviceId);
                }
            }
        }

        //判断 addDeviceMap 是否还有数据
        if (!addDeviceMap.isEmpty()) {
            //不为null，执行遍历新增操作
            for (Map.Entry<Integer, String> entry : addDeviceMap.entrySet()) {
                FmamWeighRoomFvsDevice fvsDevice = new FmamWeighRoomFvsDevice();
                fvsDevice.setWeighRoomId(weighRoomId);
                fvsDevice.setFvsDeviceId(entry.getKey());
                fvsDevice.setFvsDeviceName(entry.getValue());
                fvsDevice.setDelFlag(DelFlag.UNDELETE.getValue());
                fmamWeighRoomFvsDeviceMapper.insertSelective(fvsDevice);
            }
        }

    }

    /**
     * 磅房视频监控设备删除
     *
     * @param weighRoomId
     * @param deviceId
     */
    @Override
    public void videoDel(Integer weighRoomId, Integer deviceId) {
        fmamWeighRoomFvsDeviceMapper.deleteLogicByWeighRoomIdAndDeviceId(weighRoomId, deviceId);
    }

}
